package cn.com.fuhuitong.main.order.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.main.base.RecyclerViewHolder
import cn.com.fuhuitong.main.order.entity.OrderListEntity
import com.alibaba.android.vlayout.LayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.BASE_HTTP_URL
import cn.com.fuhuitong.app.getImageUrl
import cn.com.fuhuitong.main.order.entity.GoodsListEntity
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.item_order_details_goods.view.*
import kotlinx.android.synthetic.main.item_order_goods.view.*
import kotlinx.android.synthetic.main.item_order_list.view.*
import kotlinx.android.synthetic.main.item_tab_layout.view.*


enum class OVT(value: Int) {
    ORDER_TAB(1),
    ORDER_LIST(2)
}

class OrderTabAdapter(
    private val mContext: Context,
    private val mHelper: LayoutHelper,
    private val tabData: MutableList<String>
) : BaseDelegateAdapter<String>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_tab_layout,
    mItemViewType = OVT.ORDER_TAB.ordinal
) {

    var selectedPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    init {
        setAddDataNew(tabData)
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        holder.itemView.text_tab_layout_name.text = data[position]
        holder.itemView.text_tab_layout_name.setTextColor(
            mContext.resources.getColor(
                if (holder.layoutPosition == selectedPosition) R.color.colorMain else R.color.colorMainGrey
            )
        )
        holder.itemView.isSelected = holder.layoutPosition == selectedPosition
    }
}

class OrderListAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<OrderListEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_list,
    mItemViewType = OVT.ORDER_LIST.ordinal
) {

    init {
        addChildClickViewIds(
            R.id.text_order_time, R.id.recycler_order_goods,
            R.id.text_order_action, R.id.text_order_cancel
        )
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(itemView: View, position: Int, itemData: OrderListEntity) {
        super.onBindViewHolder(itemView, position, itemData)
        itemView.text_order_status.setText(
            when (itemData.status) {
                1 -> R.string.app_wait_pay
                2 -> R.string.app_wait_take
                3 -> R.string.app_complete
                else -> R.string.app_cancel
            }
        )
        itemView.text_order_status.setTextColor(
            mContext.resources.getColor(
                if (itemData.status == 1) R.color.colorMain else R.color.colorMainGrey
            )
        )
        itemView.text_order_time.text =
            TimeUtils.millis2String(itemData.createtime * 1000, "yyyy-MM-dd HH:mm")

        var number = 0
        itemData.goods_list.forEach {
            number += it.num.toInt()
        }
        itemView.text_order_number.text = "共${number}，实付"
        itemView.text_order_price.text = "￥${itemData.pay_price}"

        itemView.text_order_action.visibility =
            if (itemData.status == 3) View.GONE else View.VISIBLE
        itemView.divider_bottom.visibility =
            if (itemData.status == 3) View.GONE else View.VISIBLE

        itemView.text_order_action.setText(
            when (itemData.status) {
                1 -> R.string.app_order_payment
                2 -> R.string.app_order_confirm
                3 -> R.string.app_order_delete
                else -> R.string.app_order_delete
            }
        )

        itemView.text_order_cancel.visibility =
            if (itemData.status == 1) View.VISIBLE else View.GONE

        itemView.recycler_order_goods.layoutManager = LinearLayoutManager(mContext)
        itemView.recycler_order_goods.adapter = GoodsAdapter(mContext, itemData.goods_list)
    }

    private class GoodsAdapter(
        private val mContext: Context,
        private val goodsEntity: MutableList<GoodsListEntity>
    ) : RecyclerView.Adapter<RecyclerViewHolder>() {

        companion object {
            const val DEFAULT = 1
            const val MORE = 2
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
            return if (viewType == DEFAULT) {
                RecyclerViewHolder(
                    LayoutInflater.from(mContext)
                        .inflate(R.layout.item_order_goods, parent, (false))
                )
            } else {
                RecyclerViewHolder(
                    LayoutInflater.from(mContext).inflate(R.layout.item_order_more, parent, (false))
                )
            }
        }

        override fun getItemCount(): Int {
            return if (goodsEntity.size > 2) 3 else goodsEntity.size
        }

        @SuppressLint("SetTextI18n")
        override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
            val itemView: View = holder.itemView
            if (holder.itemViewType == DEFAULT) {
                Glide.with(itemView).load(getImageUrl(goodsEntity[position].image))
                    .into(itemView.image_goods_image)
                itemView.text_goods_name.text = goodsEntity[position].goods_title
                itemView.text_goods_number.text = "x${goodsEntity[position].num}"
            } else {

            }
        }

        override fun getItemViewType(position: Int): Int {
            return if (position == 2) MORE else DEFAULT
        }

    }
}